<?php

class RequestController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
			'ajaxOnly'
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array(
					// Default Controller
					'getEvent',
				),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionGetEvent()
	{
		$counter = $_POST['counterEvent'];
		
		$criteria = new CDbCriteria();
		$criteria->condition = 'status = "A" AND DATE(startdate) >= NOW()';
		$criteria->limit = 5;
		$criteria->offset = $counter * 5 + 5;
		$criteria->order = 'startdate ASC,enddate ASC';
		
		$model = Events::model()->findAll($criteria);
		
		if (!empty($model)) {		
			foreach ($model as $data) {
				$dataArr[] = array('id'=>$data->id, 'content'=>$this->renderPartial('application.modules.Events.views.default._view', array('data'=>$data), true));
			}
			
			echo CJSON::encode(array(
				'status'=>'success',
				'data'=>$dataArr
			));
		}
		else {
			echo CJSON::encode(array(
				'status'=>'limited'
			));
		}
	}
}